package com.bjx.flow.mmx.task.handler;

import cn.hutool.core.collection.ListUtil;
import com.bjx.flow.mmx.bean.MmxNextNodeData;
import com.bjx.flow.mmx.bean.MmxNodeData;
import com.bjx.flow.mmx.constant.MmxBranchConstant;
import com.bjx.flow.mmx.constant.MmxNodeConstant;
import com.bjx.flow.mmx.domain.entity.MmxFlowBranchEntity;
import com.bjx.flow.mmx.domain.entity.MmxFlowInstanceEntity;
import com.bjx.flow.mmx.domain.entity.MmxFlowTaskEntity;
import com.bjx.flow.mmx.extension.MmxAbstractApprover;
import com.bjx.flow.mmx.service.MmxFlowBranchService;
import com.bjx.flow.mmx.service.MmxFlowInstanceService;
import com.bjx.flow.mmx.service.MmxFlowTaskService;
import com.bjx.flow.mmx.task.MmxAbstractTaskHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 用户任务处理
 * @author: liushuai
 * @date: 2023/5/23 17:26
 */
@Service(MmxNodeConstant.USER_TASK + MmxNodeConstant.TASK_FLAG)
public class MmxUserTaskHandler extends MmxAbstractTaskHandler {
    @Resource
    private MmxFlowTaskService flowTaskService;
    @Resource
    private MmxAbstractApprover approverService;

    @Resource
    private MmxFlowBranchService flowBranchService;

    @Resource
    private MmxFlowInstanceService flowInstanceService;

    @Override
    public List<MmxNodeData> simulationExecute(MmxNextNodeData nextNodeData) {
        MmxNodeData nodeData = nextNodeData.getNodeDataList().get(0);
        List<String> approver = approverService.getApprover(nodeData, nextNodeData.getVariables());
        nodeData.setAssignees(approver);
        return ListUtil.toList(nodeData);
    }

    @Override
    public void passExecute(MmxNextNodeData nextNodeData) {
        //获取当前任务节点
        MmxNodeData currentNode = nextNodeData.getCurrentNode();
        //获取流程实例
        MmxFlowInstanceEntity instance = nextNodeData.getInstance();
        //下一个节点数据
        List<MmxNodeData> nextNode = this.simulationExecute(nextNodeData);
        //查看是否为会签
        Integer nodeRule = currentNode.getProperties().getCountersign();
        //判断是否为会签
        if (MmxNodeConstant.COUNTERSIGN_NO.intValue() == nodeRule) {
            //非会签节点
            MmxNodeData nodeData = nextNode.get(0);
            //根据任务id查询任务
            Long branchId = null;
            if (nextNodeData.getTaskId() != null) {
                MmxFlowTaskEntity task = flowTaskService.selectTaskByTaskId(nextNodeData.getTaskId());
                branchId = task.getBranchId();
                //修改分支节点数据
                flowBranchService.updateNodeIdByBranchId(nodeData.getProperties().getId(), task.getBranchId());
            } else {
                MmxFlowBranchEntity branch = MmxFlowBranchEntity.builder()
                        .nodeId(nodeData.getProperties().getId())
                        .instanceId(instance.getInstanceId())
                        .branchStatus(MmxBranchConstant.STATUS_ING).build();
                flowBranchService.insertBranch(branch);
                branchId = branch.getBranchId();
            }
            //根据节点id删除流程任务
            flowTaskService.deleteTaskByNodeId(currentNode.getProperties().getId());
            List<MmxFlowTaskEntity> tasks = new ArrayList<>();
            for (String assignee : nodeData.getAssignees()) {
                tasks.add(MmxFlowTaskEntity.builder()
                        .branchId(branchId)
                        .businessId(instance.getBusinessId())
                        .instanceId(instance.getInstanceId())
                        .assignee(assignee)
                        .nodeId(nodeData.getId())
                        .nodeName(nodeData.getProperties().getName())
                        .instanceName(instance.getInstanceName())
                        .build());
            }
            //批量插入流程任务
            flowTaskService.insertFlowTaskBatch(tasks);
            //根据版本和实例id更新流程实例
            int newVersion = instance.getVersion() + 1;
           // flowInstanceService.updateFlowInstanceVersion();

        } else {

        }
        //删除当前任务
        // flowTaskService.deleteTask(nextNodeData.getTaskId());
    }

    @Override
    public void rejectExecute(MmxNextNodeData nextNodeData) {

    }

    @Override
    public void sendBackExecute(MmxNextNodeData nextNodeData) {

    }

    @Override
    public void appointExecute(MmxNextNodeData nextNodeData) {

    }
}
